package ua.nure.nguyen.blinov.chapter4.a;

import java.util.ArrayList;
import java.util.List;

public class Sentence {
	private List<Word> sentence;
	
	private Sentence(List<Word> sentence) {
		this.sentence = sentence;
	}
	
	public static Sentence parseSentence(String sentence) {
		if (sentence == null || sentence.isEmpty()) {
			return new Sentence(new ArrayList<Word>());
		} else {
			List<Word> result = new ArrayList<Word>();
			String[] words = sentence.split(" ");
			for (String word : words) {
				result.add(Word.parseWord(word));
			}
			return new Sentence(result);
		}
	}
	
	@Override
	public boolean equals(Object other) {
		if (other instanceof Sentence == false) {
			return false;
		}
		List<Word> otherSentence = ((Sentence)other).sentence;
		if(sentence.size() != otherSentence.size()) {
			return false;
		}
		for (int i = 0; i < sentence.size(); i++) {
			if (!sentence.get(i).equals(otherSentence.get(i))) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	public String toString() {
		return sentence.toString();
	}
}
